package com.yst.suivi.services.folders;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

import com.yst.suivi.comparator.NameObjectComparator;
import com.yst.suivi.data.folders.Folder;
import com.yst.suivi.data.folders.FolderLocalHome;
import com.yst.suivi.data.folders.FolderUtil;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.ProjectUtil;
import com.yst.suivi.services.AbstractSessionBean;

/**
 * @ejb.bean display-name="FolderService" jndi-name="FolderHomeRemote"
 *           name="FolderView" type="Stateless" view-type="both"
 */
public class FolderServiceBean extends AbstractSessionBean implements SessionBean
{
	/**
	 * @throws CreateException
	 * @ejb.create-method view-type = "both"
	 */
	public void ejbCreate() throws CreateException
	{
		// by default nothing to do
	}

	/**
	 * This method create a new Folder.
	 * 
	 * @param standard
	 * @return standard updated
	 * @throws CreateException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public String createFolder(String nom, Project prj) throws NamingException, CreateException
	{
		checkConstraints(nom, "".intern(), prj);
		FolderLocalHome home = FolderUtil.getLocalHome();
		Folder folder = home.create();
		updateObject(nom, prj, folder);
		return folder.getId();
	}

	/**
	 * The method checks if all the business constraints are verified.
	 * 
	 * @param login
	 * @param nom
	 * @param prenom
	 */
	private void checkConstraints(String nom, String id, Project prj)
	{
		String[] parameter = { "folder", "nom" };
		checkEmptyField(nom, parameter);
		checkFieldLength(nom, SIMPlE_FIELD_MAX_LENGTH, "nom");
		StringBuffer buf = new StringBuffer();
		buf.append(" AND prjId='").append(prj.getId()).append("'");
		checkUnicity(nom, "folder", "nom", id, buf.toString());
	}

	/**
	 * This method update a Folder.
	 * 
	 * @return standard updated
	 * @throws NamingException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void storeFolder(String id, String nom) throws FinderException, NamingException
	{

		FolderLocalHome home = FolderUtil.getLocalHome();
		Folder folder = home.findByPrimaryKey(id);
		checkConstraints(nom, id, folder.getProject());
		updateObject(nom, folder.getProject(), folder);
	}

	/**
	 * La methode remplie l'objet EJB avec les valeurs fournies
	 * 
	 * @param annee
	 * @param mois
	 * @param jour
	 * @param tpsPos
	 * @param prj
	 */
	private void updateObject(String nom, Project prj, Folder folder)
	{
		folder.setNom(nom);
		folder.setProject(prj);
	}

	/**
	 * This method delete a Folder.
	 * 
	 * @param standard
	 * @throws NamingException
	 * @throws RemoveException
	 * @throws EJBException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void deleteFolder(String id) throws NamingException, EJBException, RemoveException
	{
		FolderLocalHome home = FolderUtil.getLocalHome();
		home.remove(id);
	}

	/**
	 * The method returns all the folder of the given project primary key
	 * 
	 * @param prjId
	 * @return
	 * @throws FinderException
	 * @throws NamingException
	 * @ejb.interface-method view-type = "both"
	 */
	
	public Collection findFolder(String prjId) throws FinderException, NamingException
	{
		Project prj = ProjectUtil.getLocalHome().findByPrimaryKey(prjId);
		ArrayList retour = new ArrayList(FolderUtil.getLocalHome().findByProject(prj));
		Collections.sort(retour, NameObjectComparator.comparator);
		return retour;
	}

	/**
	 * The method returns the folder identify by the primary key
	 * 
	 * @param folderKey
	 * @return
	 * @throws FinderException
	 * @throws NamingException.
	 * @ejb.interface-method view-type = "both"
	 */
	public Folder findFolderByKey(String folderKey) throws FinderException, NamingException
	{
		return FolderUtil.getLocalHome().findByPrimaryKey(folderKey);
	}
}
